package com.diploma.project.aiqiyi.common.annotation;

import feign.MethodMetadata;
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * @see AnnotatedParameterProcessor
 * @see ParamQueryMap
 */
public class ParamQueryMapParameterProcessor implements AnnotatedParameterProcessor {

    private static final Class<ParamQueryMap> ANNOTATION = ParamQueryMap.class;

    @Override
    public Class<? extends Annotation> getAnnotationType() {
        return ANNOTATION;
    }

    @Override
    public boolean processArgument(AnnotatedParameterContext context, Annotation annotation, Method method) {
        int paramIndex = context.getParameterIndex();
        MethodMetadata metadata = context.getMethodMetadata();
        if (metadata.queryMapIndex() == null) {
            metadata.queryMapIndex(paramIndex);
            metadata.queryMapEncoded(ANNOTATION.cast(annotation).encoded());
        }
        return true;
    }
}
